// pages/withdrawCash/withdrawCash.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // types:[
    //   { value: "微信", checked: true},
    //   { value: "支付宝", checked: false},
    //   { value: "银行卡", checked: true}
    // ]
  },

  getMoney(e){
    const that = this;
    e.detail.value = e.detail.value > that.data.myMoney ? that.data.myMoney : e.detail.value;
    that.setData({
      money: that.clearNoNum(e.detail.value)
    })
  },

  clearNoNum(v) {
    v = v.replace(/[^\d.]/g, "");  //清除“数字”和“.”以外的字符   
    v = v.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的   
    v = v.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    v = v.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');//只能输入两个小数   
    if (v.indexOf(".") < 0 && v != "") {//以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额  
      v = parseFloat(v);
    }
    return v
  },

  // 提交表单数据
  formSubmit(e) {
    const that = this;
    let formData = e.detail.value;
    let json = {
      token: app.globalData.token,
      account_bank: "",// formData.accountNumber,
      account_name: "",// formData.accountName,
      bank_name: "微信",// formData.bankName,
      money: that.data.money,
      verify_code: formData.code,
      unique_id: that.data.verifyCodeId
    }
    let jsonData = app.utils.getSignData(json);
    that.setData({
      hiddenLoading: false
    })
    console.log("提现申请参数:", jsonData)
    app.utils.post(app.api.reqUrls.doWidthdrawCash, jsonData).then(res => {
      that.setData({
        hiddenLoading: true
      })
      if (res.status == 1) {
        wx.showModal({
          content: res.msg,
          showCancel: false,
          confirmText: '我知道了',
          confirmColor: '#ff5151',
          success() {
            wx.navigateBack({
              delta: -1
            })
          }
        })
      }else{
        throw res.msg
      }
    }).catch(err => {
      app.showInfo(err.toString());
      that.getVerify();
    })
  },

  // 生成随机颜色
  randomColor(min, max) {
    const that = this;
    var r = that.randomNum(min, max);
    var g = that.randomNum(min, max);
    var b = that.randomNum(min, max);
    return "rgb(" + r + "," + g + "," + b + ")";
  },


   randomNum(min, max) {
    return Math.floor(Math.random() * (max - min) + min);
   },


  drawPic(str) {
    const that = this;
    const ctx = wx.createCanvasContext('code-canvas');
    /**绘制背景色**/
    ctx.fillStyle = that.randomColor(180, 240); //颜色若太深可能导致看不清
    ctx.fillRect(0, 0, 180 / 750 * that.data.windowWidth, 60 / 750 * that.data.windowWidth)
    /**绘制文字**/
    var arr;
    var text = '';
    var str = str;
    for(var i = 0; i< 4; i++) {
      var txt = str[i];
      ctx.fillStyle = that.randomColor(50, 160); //随机生成字体颜色
      ctx.font = that.randomNum(20, 26) + 'rpx SimHei'; //随机生成字体大小
      var x = 5 + i * 20;
      var y = that.randomNum(20, 25);
      var deg = that.randomNum(-20, 20);
      //修改坐标原点和旋转角度
      ctx.translate(x, y);
      ctx.rotate(deg * Math.PI / 180);
      ctx.fillText(txt, 5, 0);
      text = text + txt;
      //恢复坐标原点和旋转角度
      ctx.rotate(-deg * Math.PI / 180);
      ctx.translate(-x, -y);
    }
  /**绘制干扰线**/
    for (var i = 0; i < 4; i++) {
      ctx.strokeStyle = that.randomColor(40, 180);
      ctx.beginPath();
      ctx.moveTo(that.randomNum(0, 90), that.randomNum(0, 28));
      ctx.lineTo(that.randomNum(0, 90), that.randomNum(0, 28));
      ctx.stroke();
    }
    /**绘制干扰点**/
    for (var i = 0; i < 20; i++) {
      ctx.fillStyle = that.randomColor(0, 255);
      ctx.beginPath();
      ctx.arc(that.randomNum(0, 90), that.randomNum(0, 28), 1, 0, 2 * Math.PI);
      ctx.fill();
    }
    ctx.draw(false, function () {
      that.setData({
        text: text
      })
    });
  },


  getVerify(){
    const that = this;
    let verifyCodeId = app.globalData.token;  // 获取验证码唯一标识
    that.setData({
      verifyCodeId
    })
    let json = {
      token: app.globalData.token,
      unique_id: verifyCodeId
    }
    console.log(json)
    let jsonData = app.utils.getSignData(json);
    app.utils.post(app.api.reqUrls.getVerify, jsonData).then(res => {
      that.setData({
        hiddenLoading: true
      })
      if (res.status == 1) {
        console.log("验证码：", res.result)
        that.drawPic(res.result);
      } else {
        app.showInfo(res.msg)
      }
    }).catch(err => {
      app.showInfo(err.toString())
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    that.setData({
      windowWidth: app.globalData.systemInfo.windowWidth,
      myMoney: app.globalData.userInfo.user_money
    })
    that.getVerify();
  },

})